﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;

namespace dogd.Tests
{
	public class UnpersistableClass
	{
		[Column("PersistID")]
		public int DbKey { get; set; }
	}

	[Table("Dog")]
	public class Dog
	{
		[PrimaryKeyColumn("DogID")]
		public int ID { get; set; }

		[Column("Breed")]
		public string BreedName { get; set; }

		[IntegerColumn("Legs")]
		public int LegCount { get; set; }
	}

	
	public class TableMetadataFacts
	{
		public class TheConstructor
		{
			[Fact]
			public void WillRejectClassWithoutTableAttribute()
			{
				Assert.Throws<ArgumentException>(() =>
						new TableMetadata(
							typeof(UnpersistableClass)));
			}
		}

		public class TheNameProperty
		{
			[Fact]
			public void IsRetrievedFromAttribute()
			{
				var metadata = new TableMetadata(typeof(Dog));

				Assert.Equal("Dog", metadata.Name);
			}
		}

		public class TheMetadataTypeProperty
		{
			[Fact]
			public void MatchesTheOriginalClassType()
			{
				var metadata = new TableMetadata(typeof(Dog));

				Assert.Equal(typeof(Dog), metadata.MetadataType);
			}
		}

		public class TheColumnsCollection
		{
			[Fact]
			public void WillLoadAllAttributedColumns()
			{
				var metadata = new TableMetadata(typeof(Dog));

				Assert.Equal(3, metadata.Columns.Count);
			}
		}

		public class TheColumnNamesCollection
		{
			[Fact]
			public void WillLoadNamesInOrder()
			{
				var metadata = new TableMetadata(typeof(Dog));

				Assert.Equal("DogID", metadata.ColumnNames[0]);
				Assert.Equal("Breed", metadata.ColumnNames[1]);
				Assert.Equal("Legs", metadata.ColumnNames[2]);
			}
		}

		public class ThePrimaryKeyProperty
		{
			[Fact]
			public void IsIdentifiedFromAttribute()
			{
				var metadata = new TableMetadata(typeof(Dog));

				var key = metadata.PrimaryKey;

				Assert.NotNull(key);
				Assert.Equal("DogID", key.ColumnName);
			}
		}
	}
}
